package com.cskaoyan.config;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import com.cskaoyan.bean.common.OSS;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;

@Component
@ConfigurationProperties(prefix = "aliyun")
@Data
public class AliComponent {

    private String accessKeyId;
    private String accessKeySecret;
    private OSS oss;

    public OSSClient getOssClient() {
        return new OSSClient(oss.getEndPoint(), accessKeyId, accessKeySecret);

    }

    public PutObjectResult fileUpload(String fileName, File file) {
        OSSClient ossClient = getOssClient();
        return ossClient.putObject(oss.getBucket(), fileName, file);
    }

    public PutObjectResult fileUpload(String fileName, InputStream inputStream) {
        OSSClient ossClient = getOssClient();
        return ossClient.putObject(oss.getBucket(), fileName, inputStream);
    }

    public String getUrl(String fileName) {
        return "https://" + oss.getBucket() + "." + oss.getEndPoint() + "/" + fileName;
    }
}
